﻿
using BLL;
using IBLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApp1.登录老师账号界面;

namespace WindowsFormsApp1
{
    public partial class FrStudentManage : Form
    {
        public static DataGridView view=null;
        // List<Students_MD> Students = new List<Students_MD>();
        IFrStudentManage_IBLL FrStudent = new FrStudentManage_BLL();
        public FrStudentManage()
        {
            InitializeComponent();
            initialize();
        }
        #region 初始化
      
        #region 初始化考生
        public void initialize()
        {
            List<StudentClass_MD> class_MDs = FrStudent.SClass();
            class_MDs.Insert(0, new StudentClass_MD()
            {
                ClassId = 0,
                ClassName="全校学生"
            }) ;
            ClassSelect.DataSource= class_MDs;
            ClassSelect.DisplayMember = "ClassName";
            ClassSelect.ValueMember = "ClassId";
            DataGridView1.AutoGenerateColumns = false;
            ClassSelect_SelectedIndexChanged(null, null);
        }
        #endregion
        #endregion
     
        #region 添加考生
        private void button1_Click_1(object sender, EventArgs e)
        {
            登录老师账号界面.AddStudengts studengts=new 登录老师账号界面.AddStudengts();
            view = DataGridView1;
            DialogResult result= studengts.ShowDialog();
            if (result ==DialogResult.OK)
            {
                ClassSelect_SelectedIndexChanged(null, null);
            }
        }
        #endregion
        #region 删除考生信息
        private void DeleteStudent_Click(object sender, EventArgs e)
        {
            if (DataGridView1.Rows.Count <= 0)
            {
                return;
            }
          
            
            string s = "update Examinee_info set status=1 where StudentId='";
            string ss = "";
            foreach (DataGridViewRow item in DataGridView1.SelectedRows)
            {
                ss =s+ (int)item.Cells[0].Value+"'";
            }

           
            FrStudent.RemoveStudent(ss);
            ClassSelect_SelectedIndexChanged(null,null);
           // DataGridView1.DataSource = null;
           //  DataGridView1.DataSource = Students;
        }
        #endregion
        #region 查询考生信息
        /// <summary>
        /// 点击回车根据学号或名字查询学生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">回车</param>
        private void Query_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue!=13)
            {
                return;
            }
            if (Query.Text == "")
            {
                return;
            }
            y();
        }
        #region 修改后查询
        private void Query_TextChanged(object sender, EventArgs e)
        {
            y();
        }
        #endregion
        #region 根据班级查询
        bool s = false;
        private void ClassSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
           
            if (!s)
            {
                s = true;
                return;
            }
            string y = (int)ClassSelect.SelectedValue != 0 ? $" where  ClassName ='{ClassSelect.Text}'" : "";
      
            DataGridView1.DataSource = FrStudent.Students(y);
        }
        #endregion
        void y()
        {
            string rea = "";
            if ((int)ClassSelect.SelectedValue != 0)
            {
                rea = $" ClassId={ClassSelect.SelectedValue} and";
            }
            string reader = null;
            if (Regex.IsMatch(Query.Text, @"[0-9]"))
            {
                reader = $"   where {rea} StudentId= {Query.Text} ";
            }
            else
            {
                reader = $"  where {rea} StudentName like '%{Query.Text}%' ";
            }
          
            DataGridView1.DataSource = FrStudent.Students(reader);
        }
        #endregion
        #region 修改考生信息
        private void AlterStudent_Click(object sender, EventArgs e)
        {
            if (DataGridView1.SelectedRows.Count<=0)
            {
                return;
            }
            if (DataGridView1.SelectedRows.Count > 1)
            {
                return;
            }

            登录老师账号界面.AlterStudent alter = new 登录老师账号界面.AlterStudent((int)DataGridView1.SelectedRows[0].Cells[0].Value);
            alter.ShowDialog();
        }
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {


            if (e.RowIndex >= 0)
            {
                DataGridView1.Rows[e.RowIndex].Selected = true;
                登录老师账号界面.AlterStudent alter = new 登录老师账号界面.AlterStudent((int)DataGridView1.Rows[e.RowIndex].Cells[0].Value,"");
                alter.Show();
            }



        }
        #endregion
        #region  批量导入
        private void BulkImport_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "All Excel Files|*.xls;*.xlsx;|Excel(*.xls)|*.xls|Excel(*.xlsx)|*.xlsx";
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                string path = openFileDialog1.FileName;
                BulkImport import = new BulkImport(path);
                DialogResult result1= import.ShowDialog();
                if (result1==DialogResult.OK)
                {
                    ClassSelect.SelectedValue = 0;
                    ClassSelect_SelectedIndexChanged(null, null);
                }

            }

        }
        #endregion
        #region 选中行
        private void DataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (DataGridView1.SelectedRows.Count<0||e.RowIndex<0)
            {
                return;
            }
         
            DataGridView1.Rows[e.RowIndex].Selected = true;
        }

        private void DataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            DataGridView1.ClearSelection();
        }
        #endregion
     
        private void AddStudent_MouseEnter(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            label2.Location = new Point(label2.Location.X,button.Location.Y+10);
            label3.Location=new Point(label3.Location.X, button.Location.Y + 10);
        }

        private void DataGridView1_CellMouseClick_1(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (DataGridView1.SelectedRows.Count < 0 || e.RowIndex < 0)
            {
                return;
            }

            DataGridView1.Rows[e.RowIndex].Selected = true;
        }

        private void DataGridView1_DataBindingComplete_1(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            DataGridView1.ClearSelection();
        }
    }
}
